﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;

public partial class adminstrator_planDoctor : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sconnectionstring = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sconnectionstring);
        myConnection.Open();
        if (Session["UserName"] == null && Session["UserNo"] == null)
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登录！'); parent.location.href='../login.aspx'</script>", false);

        if (!IsPostBack)
        {
            string courseName = Request.QueryString["courseName"];
            Label1.Text = "(" + courseName + ")";
            bind();
        }
    }
    protected void Gridview1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        Gridview1.PageIndex = e.NewPageIndex;
        bind();
    }
    protected void Gridview1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string teacherNo = Gridview1.DataKeys[e.NewEditIndex][1].ToString();
        string teacherName = Gridview1.DataKeys[e.NewEditIndex][0].ToString();
        string jz = Request.QueryString["courseNo"];
        string mysql = "select * from PatCF where ID_JZ='" + jz + "' and pd is not null";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataReader myReader = mycmd.ExecuteReader();
        if (myReader.Read())
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('错误！');</script>", false);
        else
        {
            string mystr1 = "update patCF set Cname='" + teacherName + "'and Price='"+teacherNo+"' where ID_JZ='" + jz + "' ";
            mycmd = new OleDbCommand(mystr1, myConnection);
            mycmd.ExecuteNonQuery();

            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>alert('成功分配医生！');location.href='planGH.aspx';</script>", false);

          
        }

    }

    public void bind()
    {
        string mysql = "select * from ChargeTb order by ID ";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();
        Adapter.SelectCommand = mycmd;
        DataSet myDS = new DataSet();
        Adapter.Fill(myDS);
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)
        {
            Gridview1.DataSource = myDS;
            Gridview1.DataKeyNames = new string[] { "Cname", "Price" };
            Gridview1.DataBind();
        }
    }
}